package com.achievement.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.BookAuthorDTO;
import com.achievement.domain.entity.BookAuthorEntity;
import com.achievement.domain.vo.BookAuthorVO;
import com.achievement.mapper.BookAuthorMapper;
import com.achievement.mappings.BookMapping;
import com.achievement.service.IBookAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_book_author(科研成果-学术著作列表-作者)】的数据库操作Service实现
* @createDate 2025-02-21 16:56:29
*/
@Service
public class BookAuthorServiceImpl extends ServiceImpl<BookAuthorMapper, BookAuthorEntity>
    implements IBookAuthorService {

    @Autowired
    private BookMapping mapping;

    /**
     * 根据著作id，查询相关作者信息
     *
     * @param id id
     */
    @Override
    public List<BookAuthorVO> getInfoByBookId(String id) {
        LambdaQueryWrapper<BookAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookAuthorEntity::getBookId,id);
        queryWrapper.orderByAsc(BookAuthorEntity::getOrderId);

        List<BookAuthorEntity> list = this.list(queryWrapper);

        List<BookAuthorVO> voList = list.stream().map(mapping::authorEntity2Vo).collect(Collectors.toList());
        return voList;
    }


    /**
     * 删除作者
     * 根据论文 bookid，删除作者信息
     * 
     * @param id id
     */
    @Override
    public boolean deleteByBookId(String id) {
        //根据论文 bookid，删除作者信息
        LambdaQueryWrapper<BookAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookAuthorEntity::getBookId,id);

        return this.baseMapper.delete(queryWrapper)>0;
    }

    /**
     * 新增作者
     *
     * @param dto List<BookAuthorDTO>
     */
    @Override
    public boolean add(List<BookAuthorDTO> dto) {
        List<BookAuthorEntity> entityList = dto.stream().map(mapping::bookDto2BookEntity).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(entityList)){
            return this.saveBatch(entityList);
        }

        return true;
    }

    /**
     * 删除作者
     * 根据论文 bookidList，删除作者信息
     *
     * @param ids List<String>
     */
    @Override
    public boolean deleteByBookIdList(List<String> ids) {
        if (CollUtil.isEmpty(ids)){
            return true;
        }

        //根据论文 paperid，删除作者信息
        LambdaQueryWrapper<BookAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BookAuthorEntity::getBookId,ids);

        return this.baseMapper.delete(queryWrapper)>0;
    }
}




